package com.resou.reader.mine.rechargerecord;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.resou.reader.R;
import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.CommonData;
import com.resou.reader.api.entry.PayRecordBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.PayService;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.mine.IView.UserInstance;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RechargeRecordActivity extends BaseActivity {

    CompositeDisposable compositeDisposable = new CompositeDisposable();

    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.no_recharge_view)
    ImageView noRechargeView;
    @BindView(R.id.rlContent)
    RelativeLayout rlContent;

    private RechargeAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private int page = 0;
    private boolean hasMore = true;
    private PayService mService;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recharge_record;
    }

    @Override
    protected void initPresenter() {
        mService = ApiImp.getInstance().getService(PayService.class);
    }

    @Override
    protected void initView() {
        mSwipeRefreshLayout.setEnabled(false);
        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.btn_login_highlight));
        mSwipeRefreshLayout.setRefreshing(true);
        mAdapter = new RechargeAdapter(this);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (mLayoutManager.findLastVisibleItemPosition() == mAdapter.getItemCount() - 1) {
                        getMoreData();
                    }
                }
            }
        });
//        test();
        getMoreData();
    }

    @Override
    protected void retryRefreshData() {
        getMoreData();
    }


    private void getMoreData() {
        if (!hasMore) {
            return;
        }
        mSwipeRefreshLayout.setRefreshing(true);
        mService.rechargeRecordList(UserInstance.getToken(), page, "200").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<CommonData<List<PayRecordBean>>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(Result<CommonData<List<PayRecordBean>>> commonDataResult) {
                        if (commonDataResult.getData() != null && commonDataResult.getData().getData() != null) {
                            List<PayRecordBean> list = commonDataResult.getData().getData();
                            if ((list == null && page == 0) || (list.size() == 0 && page == 0)) {
                                noRechargeView.setVisibility(View.VISIBLE);
                                rlContent.setVisibility(View.GONE);
                            } else {
                                rlContent.setVisibility(View.VISIBLE);
                                noRechargeView.setVisibility(View.GONE);
                            }
                            hasMore = list.size() == 200;
                            mAdapter.updateList(list, hasMore);
                            page++;
                        } else {
                            noRechargeView.setVisibility(View.VISIBLE);
                            rlContent.setVisibility(View.GONE);
                        }
                        mSwipeRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mSwipeRefreshLayout.setRefreshing(false);
                        ToastUtil.makeShortToast("网络异常");
                        showNetErrorView(R.id.rlContent);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public Context getViewContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.clear();
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, RechargeRecordActivity.class);
        context.startActivity(intent);
    }
}
